﻿Imports System.Data
Imports System.Data.OleDb


Public Class Form1
    Public conexion As New OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=..\..\..\Agenda.accdb")
    Public adapter As New OleDbDataAdapter("Select * from Tabla1", conexion)
    Public midataset As New DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        adapter.Fill(midataset, "Tabla1")
        DataGridView1.DataSource = midataset
        DataGridView1.DataMember = "Tabla1"
        txtNombre.DataBindings.Add("Text", midataset, "Tabla1.nombre")
        txtApellidos.DataBindings.Add("Text", midataset, "Tabla1.apellidos")
        txtTelefono.DataBindings.Add("Text", midataset, "Tabla1.telefono")
        txtCod.DataBindings.Add("Text", midataset, "Tabla1.cod")
        TextBox5.DataBindings.Add("Text", midataset, "Tabla1.observaciones")
        registro()

    End Sub

    Private Sub Siguiente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Siguiente.Click
        BindingContext(midataset, "Tabla1").Position += 1
        registro()

    End Sub

    Private Sub Anterior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Anterior.Click
        BindingContext(midataset, "Tabla1").Position -= 1
        registro()

    End Sub

    Private Sub Primero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Primero.Click
        BindingContext(midataset, "Tabla1").Position = 0
        registro()

    End Sub


    Private Sub Ultimo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ultimo.Click
        BindingContext(midataset, "Tabla1").Position = DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Displayed)
        registro()

    End Sub

    Private Sub registro()
        Label6.Text = ((Me.BindingContext(midataset, "Tabla1").Position + 1).ToString + " de ") + Me.BindingContext(midataset, "Tabla1").Count.ToString
        If (Me.BindingContext(midataset, "Tabla1").Position + 1 = Me.BindingContext(midataset, "Tabla1").Count) Then
            Siguiente.Enabled = False
            Ultimo.Enabled = False
        Else
            Siguiente.Enabled = True
            Ultimo.Enabled = True
        End If

        If (Me.BindingContext(midataset, "Tabla1").Position = 0) Then
            Primero.Enabled = False
            Anterior.Enabled = False
        Else
            Primero.Enabled = True
            Anterior.Enabled = True
        End If
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        registro()
    End Sub


    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
        Me.Close()

    End Sub

    Private Sub NuevoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevoToolStripMenuItem.Click
        txtNombre.Text = ""
        txtApellidos.Text = ""
        txtTelefono.Text = ""
        txtCod.Text = ""
        TextBox5.Text = ""
        txtNombre.Focus()

    End Sub

    Private Sub GuardarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuardarToolStripMenuItem.Click
        Dim cb As New OleDbCommandBuilder(adapter)
        adapter.InsertCommand = cb.GetInsertCommand

        Dim mitabla As DataTable = midataset.Tables("Tabla1")
        Dim dc As DataRowCollection = mitabla.Rows
        Dim nuevafila As DataRow = mitabla.NewRow

        'nuevafila.Item("nombre") = txtNombre.Text
        'nuevafila.Item("apellidos") = txtApellidos.Text
        'nuevafila.Item("telefono") = txtTelefono.Text
        'nuevafila.Item("observaciones") = TextBox5.Text
        dc.Add(txtCod.Text, txtNombre.Text, txtApellidos.Text, txtTelefono.Text, TextBox5.Text)

        adapter.Update(mitabla)
        midataset.Clear()
        adapter.Fill(midataset, "Tabla1")
        DataGridView1.DataSource = midataset
        DataGridView1.DataMember = "Tabla1"
        registro()

    End Sub


    'PABLO ES TONTO :D
    Private Sub ModificarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Dim cb As New OleDbCommandBuilder(adapter)
        adapter.UpdateCommand = cb.GetUpdateCommand

        Dim a As Integer = BindingContext(midataset, "Tabla1").Position
        Dim fila As DataRow = midataset.Tables("Tabla1").Rows(a)
        fila.BeginEdit()
        fila("nombre") = txtNombre.Text
        fila("apellidos") = txtApellidos.Text
        fila("telefono") = txtTelefono.Text
        fila("observaciones") = TextBox5.Text
        fila.EndEdit()

        adapter.Update(midataset, "Tabla1")
        midataset.Clear()
        adapter.Fill(midataset, "Tabla1")


    End Sub

    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Dim cb As New OleDbCommandBuilder(adapter)
        adapter.DeleteCommand = cb.GetDeleteCommand
        'hacer hueco dnd se van a meter los datos
        Dim mitabla As DataTable = midataset.Tables("Tabla1")
        Dim res As Integer
        res = MsgBox("¿Esta seguro de que quieres borrar?")
        If res = vbOK Then
            If BindingContext(midataset, "Tabla1").Count > 0 Then
                BindingContext(midataset, "Tabla1").RemoveAt(BindingContext(midataset, "Tabla1").Position)
            End If
        End If
        'actualizas la tabla y borras los datos anteriores
        adapter.Update(mitabla)
        midataset.Clear()
        adapter.Fill(midataset, "Tabla1")
        'actualizas el dataGridView
        DataGridView1.DataSource = midataset
        DataGridView1.DataMember = "Tabla1"


    End Sub

    Private Sub BuscarToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuscarToolStripMenuItem1.Click
        Dim pos As Integer = 0
        Dim m As Integer
        conexion.Open()
        Dim cmd As New OleDbCommand("select * from Tabla1", conexion)
        Dim dr As OleDbDataReader = cmd.ExecuteReader
        Do While dr.Read
            If dr.Item("apellidos") = txtApellidos.Text Then
                BindingContext(midataset, "Tabla1").Position = pos
                m = 1
            Else
                pos = pos + 1
            End If
        Loop
        If m = 0 Then
            MsgBox("Búsqueda fallida")
        End If
        conexion.Close()
        'esto hace una cosa rarisima con el datagrid q nadie sabe arreglar, pero la base de datos no se cambia

    End Sub

    Private Sub BúsquedaAvanzadaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BúsquedaAvanzadaToolStripMenuItem.Click
        Form2.Show()

    End Sub

End Class
